package org.jeecg.modules.demo.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.dto.AdditionalStatusCount;
import org.jeecg.modules.demo.crm.entity.CAdditionalRequest;
import org.jeecg.modules.demo.crm.mapper.CAdditionalRequestMapper;
import org.jeecg.modules.demo.crm.mapper.CQuotationMapper;
import org.jeecg.modules.demo.crm.service.ICAdditionalRequestService;
import org.jeecg.modules.demo.crm.service.ICCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 增项需求表
 * @Author: jeecg-boot
 * @Date:   2025-06-18
 * @Version: V1.0
 */
@Service
public class CAdditionalRequestServiceImpl extends ServiceImpl<CAdditionalRequestMapper, CAdditionalRequest> implements ICAdditionalRequestService {

    @Autowired
    private CAdditionalRequestMapper cAdditionalRequestMapper;

    @Autowired
    private ICCommonService cCommonService;

    @Autowired
    private CQuotationMapper cQuotationMapper;

    @Override
    public CAdditionalRequest latestRequirements(String subProjectId) {
        CAdditionalRequest dto = cAdditionalRequestMapper.findBySubProjectId(subProjectId);
        return dto;
    }

    @Override
    public AdditionalStatusCount statusCount(CAdditionalRequest cAdditionalRequest, HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CAdditionalRequest query = new CAdditionalRequest();
        Boolean permissionCheck = cCommonService.permissionCheck();
        if(permissionCheck){
            query.setAssignUser(sysUser.getId());
            if("-1".equals(cAdditionalRequest.getProjectId())){
                cAdditionalRequest.setProjectId(null);
            }
            if("-1".equals(cAdditionalRequest.getSubProjectId())){
                cAdditionalRequest.setSubProjectId(null);
            }
        }
        if(cAdditionalRequest.getProjectId() != null){
            query.setProjectId(cAdditionalRequest.getProjectId());
        }
        if(cAdditionalRequest.getSubProjectId() != null){
            query.setSubProjectId(cAdditionalRequest.getSubProjectId());
        }

        List<String> ids = new ArrayList<>();
        if(cAdditionalRequest.getQuotationStatus()!= null && cAdditionalRequest.getQuotationStatus().length()>0){
            if("已报价".equals(cAdditionalRequest.getQuotationStatus())){
                ids = cQuotationMapper.additionalRelatedQuotation();
            }else if ("未报价".equals(cAdditionalRequest.getQuotationStatus())){
                ids = cQuotationMapper.additionalNotRelatedQuotation();
            }
        }


        //全部
        QueryWrapper<CAdditionalRequest> queryWrapper = QueryGenerator.initQueryWrapper(query, req.getParameterMap());
        if(ids.size()>0) queryWrapper.in("id", ids);
        Long totalStatusCount = cAdditionalRequestMapper.selectCount(queryWrapper);

        //报价中
        queryWrapper = QueryGenerator.initQueryWrapper(query, req.getParameterMap());
        if(ids.size()>0) queryWrapper.in("id", ids);
        queryWrapper.eq("status", "创建成功");
        Long createStatusCount = cAdditionalRequestMapper.selectCount(queryWrapper);

        //报价中
        queryWrapper = QueryGenerator.initQueryWrapper(query, req.getParameterMap());
        if(ids.size()>0) queryWrapper.in("id", ids);
        queryWrapper.eq("status", "报价中");
        Long quoteStatusCount = cAdditionalRequestMapper.selectCount(queryWrapper);

        //待处理
        queryWrapper = QueryGenerator.initQueryWrapper(query, req.getParameterMap());
        if(ids.size()>0) queryWrapper.in("id", ids);
        queryWrapper.eq("status", "待处理");
        Long pendingStatusCount = cAdditionalRequestMapper.selectCount(queryWrapper);

        //成立
        queryWrapper = QueryGenerator.initQueryWrapper(query, req.getParameterMap());
        if(ids.size()>0) queryWrapper.in("id", ids);
        queryWrapper.eq("status", "成立");
        Long establishedStatusCount = cAdditionalRequestMapper.selectCount(queryWrapper);

        //驳回
        queryWrapper = QueryGenerator.initQueryWrapper(query, req.getParameterMap());
        if(ids.size()>0) queryWrapper.in("id", ids);
        queryWrapper.eq("status", "驳回");
        Long rejectedStatusCount = cAdditionalRequestMapper.selectCount(queryWrapper);


        AdditionalStatusCount dto = AdditionalStatusCount.builder()
                .totalStatusCount(totalStatusCount)
                .quoteStatusCount(quoteStatusCount)
                .pendingStatusCount(pendingStatusCount)
                .establishedStatusCount(establishedStatusCount)
                .rejectedStatusCount(rejectedStatusCount)
                .createStatusCount(createStatusCount)
                .build();
        return dto;
    }
}
